<template>
    <div class="login-container">
        <div class="login-bac">
            <img src="../assets/images/login.png" />
        </div>
        <el-card class="login-box">
            <div class="login-box-logo">
                <img src="../assets/images/logo.jpg"/>
                <h1>沐晨校园管理</h1>
            </div>
            <div class="login-box-form">
                <h1>管理员登录</h1>
                <el-form ref="form" :model="formData" :rules="formRules" label-position="left" label-width="80px">
                    <el-form-item label="用户名" prop="userName">
                        <el-input v-model="formData.userName" placeholder="测试用户名admin123"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="formData.password" show-password placeholder="测试密码admin123"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="login" type="primary">登录</el-button>
                        <el-button @click="register" type="primary">注册</el-button>
                        <el-button @click="seset" type="info">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
    </div>
</template>

<script>
import axios from 'axios'

export default {

    data () {
        return {
            formData: {
                userName: 'admin123',
                password: 'admin123'
            },
            formRules: {
                userName: [
                    {required: true, trigger: 'change', message: '请输入用户名'}
                ],
                password: [
                    {required: true, trigger: 'change', message: '请输入密码'}
                ]
            }
        }
    },
    methods: {
        login () {
            this.$refs.form.validate(valid => {
                if (valid) {
                    axios({
                        url: 'http://127.0.0.1:3000/api/users/login',
                        method: 'post',
                        data: {
                            ...this.formData
                        }
                    }).then(res => {
                        console.log(res)
                        if (res.data.code === 200) {
                            this.$notify.success({
                                title: '登录成功'
                            })
        
                            setTimeout(() => {
                                this.$router.push({
                                    path: '/home'
                                })
                            }, 500)
                        } else {
                            this.$notify.error({
                                title: '登录失败'
                            })
                        }
                    })
                } else {
                    return false
                }
            })
        },
        register () {
            this.$router.push({
                path: '/register'
            })
        },
        seset () {
            this.$refs.form.resetFields()
        }
    }
}
</script>

<style lang="scss" scoped>
    .login-container {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .login-bac {
            width: 60%;
            height: 80%;
            margin-left: 50px;
            box-sizing: border-box;
            img {
                width: 100%;
                height: 100%;
            }
        }

        .login-box {
            background: rgb(255, 255, 255);
            width: 30%;
            height: 80%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            margin-right: 50px;
            box-sizing: border-box;

            .login-box-logo {
                display: flex;
                justify-content: center;
                align-items: center;
                margin-top: -100px;
                margin-bottom: 50px;
                box-sizing: border-box;
                img {
                    width: 100px;
                    height: 50px;
                }
                h1 {
                    font-size: 50px;
                    color: #00BFFF;
                }
            }

            .login-box-form {
                h1 {
                    text-align: center;
                    color: #329cfc;
                }
            }
        }
    }
</style>